<?php

namespace Controller;

require_once('Validator/Validator.php');

class MovieController{
	
	public function doControll(\Model\Database $db, \Model\MovieHandler $movieHandler){
		
		$userView = new \View\UserView();
		$menuView = new \View\MenuView();
		$movieView = new \View\MovieView();
		$validator = new \Validator\Validator();
        
		$out="";   
		$out .= $menuView->MyPageButton();
		//Hämtar vald film     
		$movie = \View\NavigationView::getCurrentmovie($movieHandler, $db);
     	//Visar vald film
		$out .= $userView->showMovie($movie);
        
		$out .= $movieView->EditMovieButton();
		
		//Redigera film
		if ($movieView->TriedToEdit()){
	
			$out .= $movieView->EditMovieBox($movie);
		}
	
			//Har användaren försökt att lägga till en film?
		if ($movieView->TriedToSaveChanges()){
			//Kontrollera indatat
			if ($validator->ValidateNotEmpty($movieView->GetName()) && $validator->ValidateRating($movieView->GetRating())
				&& $validator->ValidateNotEmpty($movieView->GetReview())) {
				//lägger till film
				if ($movieHandler->EditMovie($movieView->GetName(),$movieView->GetReview(), $movieView->GetRating(),$movie-> getMovieId())){
					$out .= $movieView->EditMovieSuccess();
					header("Location: index.php?MovieId=". $movie->getMovieId());
				}
			} else {
				$out .= $movieView->EditMovieBox($movie);
			}
			$out .=$validator->GetValidationError();
		} else {
			
			
		}
		
        return $out;
		
	}
}
